package com.quanquan.management.controller;

import base.BaseConst;
import com.quanquan.management.dto.UserLevelDTO;
import com.quanquan.management.request.UserLevelRequestBody;
import com.quanquan.management.service.UserLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:SquareController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/11 10:40
 * @Version 1.0
 */
@Controller
@RequestMapping("/user_level")
public class UserLevelController extends ManagerBaseController{

    private final UserLevelService userLevelService;

    @Autowired
    public UserLevelController(UserLevelService userLevelService) {
        this.userLevelService = userLevelService;
    }

    /**
     * 查看列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model) {
        List<UserLevelDTO> list = userLevelService.list();
        model.addAttribute("pageObj",list);
        System.out.println(list);
        return "userLevel/list";
    }



    /**
     * 编辑
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(UserLevelRequestBody requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            UserLevelDTO byId = userLevelService.getById(requestBody.getUserLevelId());
            System.out.println(byId);
            model.addAttribute("editable",byId);
            return "userLevel/form";
        }
        boolean add = userLevelService.update(requestBody);
        if (add) {
            return "redirect:/user_level/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

}
